package com.tzstcl.sys.user.api;

import com.alibaba.fastjson.JSON;
import com.tzstcl.commons.utils.StringUtils;
import com.tzstcl.base.model.AjaxResult;
import com.tzstcl.base.model.BaseAPIModel;
import com.tzstcl.framework.redis.RedisService;
import com.tzstcl.sys.user.model.SysDept;
import com.tzstcl.sys.user.model.SysDeptPost;
import com.tzstcl.sys.user.model.SysPosition;
import com.tzstcl.sys.user.model.SysPost;
import com.tzstcl.sys.user.model.SysUser;
import com.tzstcl.sys.user.service.SysDeptService;
import com.tzstcl.sys.user.service.SysPositionService;
import com.tzstcl.sys.user.service.SysUserService;
import com.tzstcl.sys.user.service.impl.SysDeptPostService;
import com.tzstcl.sys.user.service.impl.SysPostService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

/**
 * 公司：天筑科技股份有限公司
 * 作者：zgq
 * 日期：2019年01月03日
 * 说明：
 */
@RestController
@RequestMapping("/api")
@Slf4j
public class AdminAPI {
    @Autowired
    private RedisService redisService;
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysDeptPostService sysDeptPostService;
    @Autowired
    private SysDeptService sysDeptService;
    @Autowired
    private SysPostService sysPostService;
    @Autowired
    private SysPositionService sysPositionService;

    /**
     * 登陆页面
     *
     * @return
     */
    @PostMapping("/login")
    public AjaxResult index(BaseAPIModel apiModel, MultipartFile file) {
        //参数校验
        String paramStr = apiModel.getData();
        if (StringUtils.isBlank(paramStr)) {
            return AjaxResult.error("请求参数空异常");
        }
        //获取参数
        SysUser user = null;
        try {
            user = JSON.parseObject(paramStr, SysUser.class);
        } catch (Exception e) {
            return AjaxResult.error("请求参数转换异常");
        }
        if (null == user || StringUtils.isBlank(user.getLoginName()) || StringUtils.isBlank(user.getPassword())) {
            return AjaxResult.error("请求参数异常");
        }
        String password = new Sha256Hash(user.getPassword()).toHex();
        SysUser srcUser = new SysUser();
        srcUser.setLoginName(user.getLoginName());
        SysUser targetUser = sysUserService.findByNameOrPass(srcUser);

        if (null == targetUser || (!targetUser.getPassword().equals(password))) {
            return AjaxResult.error("用户名或密码错误");
        }

        SysDeptPost sysDeptPost = sysDeptPostService.get(targetUser.getId());

        if (sysDeptPost != null) {
            SysPost post = new SysPost();
            post.setId(sysDeptPost.getPostId());

            SysPost sysPost = sysPostService.get(post);
            if (sysPost != null) {
                targetUser.setPost(sysPost);
                targetUser.setPostId(sysPost.getId());
            }

            SysDept dept = new SysDept();
            dept.setId(sysDeptPost.getDeptId());

            SysDept sysDept = sysDeptService.get(dept);
            if (sysDept != null) {
                targetUser.setDept(sysDept);
                targetUser.setDeptId(sysDept.getId());
            }
            SysPosition sysPosition = sysPositionService.getOne(sysDeptPost.getPositionId());
            if (null != sysPosition) {
                targetUser.setPosition(sysPosition);
                targetUser.setPositionId(sysPosition.getId());
            }
        }
        String uuid = StringUtils.uuid();
        redisService.set(uuid, targetUser, 24 * 60 * 60L);
        //登录用户验证
        targetUser.setPassword(null);
        targetUser.setLoginDate(null);
        targetUser.setLoginFlag(null);
        targetUser.setLoginIp(null);
//        targetUser.setCreateDate(null);
        Map data = new HashMap();
        data.put("token", uuid);
        data.put("user", targetUser);
        return AjaxResult.success("登录成功", data);

    }


}
